home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / lxml / _elementpath.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  5KB  |  226 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import re
  5. xpath_tokenizer = re.compile('(\'[^\']*\'|"[^"]*"|::|//?|\\.\\.|\\(\\)|[/.*:\\[\\]\\(\\)@=])|((?:\\{[^}]+\\})?[^/:\\[\\]\\(\\)@=\\s]+)|\\s+').findall
  6.  
  7. def prepare_tag(next, token):
  8.     tag = token[1]
  9.     
  10.     def select(result):
  11.         for elem in result:
  12.             for e in elem.iterchildren(tag = tag):
  13.                 yield e
  14.             
  15.         
  16.  
  17.     return select
  18.  
  19.  
  20. def prepare_star(next, token):
  21.     
  22.     def select(result):
  23.         for elem in result:
  24.             for e in elem:
  25.                 yield e
  26.             
  27.         
  28.  
  29.     return select
  30.  
  31.  
  32. def prepare_dot(next, token):
  33.     
  34.     def select(result):
  35.         return result
  36.  
  37.     return select
  38.  
  39.  
  40. def prepare_iter(next, token):
  41.     token = next()
  42.     if token[0] == '*':
  43.         tag = '*'
  44.     elif not token[0]:
  45.         tag = token[1]
  46.     else:
  47.         raise SyntaxError
  48.     
  49.     def select(result):
  50.         for elem in result:
  51.             for e in elem.iterdescendants(tag = tag):
  52.                 yield e
  53.             
  54.         
  55.  
  56.     return select
  57.  
  58.  
  59. def prepare_dot_dot(next, token):
  60.     
  61.     def select(result):
  62.         for elem in result:
  63.             parent = elem.getparent()
  64.             if parent is not None:
  65.                 yield parent
  66.                 continue
  67.         
  68.  
  69.     return select
  70.  
  71.  
  72. def prepare_predicate(next, token):
  73.     token = next()
  74.     if token[0] == '@':
  75.         token = next()
  76.         if token[0]:
  77.             raise SyntaxError('invalid attribute predicate')
  78.         
  79.         key = token[1]
  80.         token = next()
  81.         if token[0] == ']':
  82.             
  83.             def select(result):
  84.                 for elem in result:
  85.                     if elem.get(key) is not None:
  86.                         yield elem
  87.                         continue
  88.                 
  89.  
  90.         elif token[0] == '=':
  91.             value = next()[0]
  92.             if value[:1] == "'" or value[:1] == '"':
  93.                 value = value[1:-1]
  94.             else:
  95.                 raise SyntaxError('invalid comparison target')
  96.             token = next()
  97.             
  98.             def select(result):
  99.                 for elem in result:
  100.                     if elem.get(key) == value:
  101.                         yield elem
  102.                         continue
  103.                 
  104.  
  105.         
  106.         if token[0] != ']':
  107.             raise SyntaxError('invalid attribute predicate')
  108.         
  109.     elif not token[0]:
  110.         tag = token[1]
  111.         token = next()
  112.         if token[0] != ']':
  113.             raise SyntaxError('invalid node predicate')
  114.         
  115.         
  116.         def select(result):
  117.             for elem in result:
  118.                 if find(elem, tag) is not None:
  119.                     yield elem
  120.                     continue
  121.             
  122.  
  123.     else:
  124.         raise SyntaxError('invalid predicate')
  125.     return select
  126.  
  127. ops = {
  128.     '': prepare_tag,
  129.     '*': prepare_star,
  130.     '.': prepare_dot,
  131.     '..': prepare_dot_dot,
  132.     '//': prepare_iter,
  133.     '[': prepare_predicate }
  134. _cache = { }
  135.  
  136. def _build_path_iterator(path):
  137.     
  138.     try:
  139.         return _cache[path]
  140.     except KeyError:
  141.         pass
  142.  
  143.     if len(_cache) > 100:
  144.         _cache.clear()
  145.     
  146.     if path[:1] == '/':
  147.         raise SyntaxError('cannot use absolute path on element')
  148.     
  149.     stream = iter(xpath_tokenizer(path))
  150.     
  151.     try:
  152.         _next = stream.next
  153.     except AttributeError:
  154.         
  155.         def _next():
  156.             return next(stream)
  157.  
  158.     except:
  159.         (None,)
  160.  
  161.     token = _next()
  162.     selector = []
  163.     while None:
  164.         
  165.         try:
  166.             selector.append(ops[token[0]](_next, token))
  167.         except StopIteration:
  168.             (None,)
  169.             (None,)
  170.             raise SyntaxError('invalid path')
  171.         except:
  172.             (None,)
  173.  
  174.         
  175.         try:
  176.             token = _next()
  177.             if token[0] == '/':
  178.                 token = _next()
  179.         continue
  180.         except StopIteration:
  181.             (None,)
  182.             (None,)
  183.             break
  184.             continue
  185.         
  186.  
  187.         return selector
  188.  
  189.  
  190. def iterfind(elem, path):
  191.     selector = _build_path_iterator(path)
  192.     result = iter((elem,))
  193.     for select in selector:
  194.         result = select(result)
  195.     
  196.     return result
  197.  
  198.  
  199. def find(elem, path):
  200.     it = iterfind(elem, path)
  201.     
  202.     try:
  203.         
  204.         try:
  205.             _next = it.next
  206.         except AttributeError:
  207.             return next(it)
  208.  
  209.         return _next()
  210.     except StopIteration:
  211.         return None
  212.  
  213.  
  214.  
  215. def findall(elem, path):
  216.     return list(iterfind(elem, path))
  217.  
  218.  
  219. def findtext(elem, path, default = None):
  220.     el = find(elem, path)
  221.     if el is None:
  222.         return default
  223.     else:
  224.         return el.text
  225.  
  226.